import { Component, OnInit, OnChanges, EventEmitter,Input,Output, SimpleChanges } from '@angular/core';

@Component({
  selector: 'el-pagination-pager',
  templateUrl: './pager.component.html'
})
export class PagerComponent implements OnInit,OnChanges {
    @Input() current: number=1;
    @Input() count: number=0;
    @Output() next: EventEmitter<number> = new EventEmitter();
    pagers: Array<number> = [];
    showPrevMore: boolean = false;
    showNextMore: boolean =false;
    quicknextIconClass: string='el-icon-more';
    quickprevIconClass: string='el-icon-more';
  constructor() { }

  ngOnChanges(changes: SimpleChanges): void {
    if (!changes)
            return;
        this.pagers = this.makePagers(this.current, this.count);
  }

  ngOnInit(): void {
    this.pagers = this.makePagers(this.current, this.count);
  }

  pagerGenerator(minValue:number){
    var /** @type {?} */ target = new Array(5).fill('').map(function (v, i) { return i + minValue; });
        return target;
  }

  makePagers(current:number, count:number){
    var /** @type {?} */ pagerCount = 7;
        if (count < pagerCount) {
            this.setMoreBtn(false, false);
            var /** @type {?} */ target = this.pagerGenerator(2);
            target.length = count - 2 >= 0 ? count - 2 : 0;
            return target;
        }
        var /** @type {?} */ max = current + 2;
        var /** @type {?} */ min = current - 2;
        if (max >= count) {
            this.setMoreBtn(true, false);
            return this.pagerGenerator(count - 5);
        }
        if (min <= 2) {
            this.setMoreBtn(false, true);
            return this.pagerGenerator(2);
        }
        this.setMoreBtn(true, true);
        return this.pagerGenerator(min);
  }


  setMoreBtn(prev:boolean, next:boolean){
    this.showPrevMore = prev;
    this.showNextMore = next;
  }

  clickHandle(to:number) {
    var /** @type {?} */ step = to - this.current;
    this.next.emit(step);
  }

  jumpHandle(step:number){
    this.next.emit(step);
  }
}
